/*
 * ClassName: Ajax
 * Parameter: url;
 * Parameter: callback;
 * Date: 2007:03:08
 */
function Ajax(url ,callback)
{
	var httpRequest;
	var debug;
	var url;
	var contentType;
	try{
		this.httpRequest = null;
		this.debug  = false;
		this.url = url;
		this.contentType = "text/xml";
		this.httpRequest = this.createXMLHttpRequest();
		
		if(this.httpRequest == null){
			this._debug("XMLHttpRequest object create failure!");
			return;
		}
		
		var xhReq = this.httpRequest;
		xhReq.onreadystatechange = function(){
			Ajax._onReadyStateChange( xhReq, callback );
		}
	}
	catch(e){
		this._debug( "unknow err: " + e.message );
	}
}

Ajax.prototype.Get = function() {
	this.setContentType( "text/html" );
	this._get();
}

Ajax.prototype.Post = function( arrKey, arrValue ) {
	var data = '';
	this.setContentType( "application/x-www-form-urlencoded" );
	for( var i=0; i<arrKey.length; i++) {
		data += "&" + escape(arrKey[i]) + "=" + arrValue[i];
	}
	data = data.replace(/^&/g, "");
	
	this._post(data);
}

Ajax.prototype.init = function() {
}

Ajax.prototype.setUrl = function( url ) {
	this.url = url;
}

Ajax.prototype.setContentType = function( type ) {
	this.contentType = type;
}

Ajax.prototype.createXMLHttpRequest = function() {
	try{return new ActiveXObject("Msxml2.XMLHTTP");}catch(e){}
	try{return new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}
	try{return new XMLHttpRequest();} catch(e){}
	return null;
}

Ajax.prototype._debug = function(message) {
	if(this.debug){
		alert(message);
	}
}

Ajax._onReadyStateChange = function( xreq, callback ){
  if(xreq == null )return;
  if(xreq.readyState == 4){
    if(xreq.status == 200){
			//alert(xreq.responseText);
			callback(this.setValue(xreq.responseText));
    }
    else{
			document.write(xreq.responseText);
		}
  }
}

Ajax.prototype._sendRequest = function(HttpMethod, data){
	this._debug( 'Send Request ' + HttpMethod + data );
	if(this.httpRequest != null){
		this.httpRequest.open(HttpMethod,this.url,true);
		if(this.contentType != null){
			this.httpRequest.setRequestHeader("Content-Type", this.contentType);
		}
		this.httpRequest.send(data);
		return true;
	}
	return false;
}

Ajax.prototype._get = function(){
	this._debug('GET');
	return this._sendRequest("GET",null);
}

Ajax.prototype._post = function(data) {
	this._debug('POST');
	return this._sendRequest("POST",data);
}

Ajax.setValue = function ( objXML ) {
	return objXML;
}
